/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import models.Var;

/**
 *
 * @author Béo
 */
public class viewLoading extends GameCanvas {

    private Graphics g;
    private Sprite sprLoad;
    private Image imgLoad, imgLogo;
    private LayerManager manager = new LayerManager();

    public viewLoading() {
        super(false);
        g = getGraphics();
        try {
            imgLoad = Image.createImage("/images/load.png");
            imgLogo = Image.createImage("/images/logo.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        setFullScreenMode(true);
        sprLoad = new Sprite(imgLoad, 18, 18);
        sprLoad.setFrameSequence(new int[]{0, 1, 2, 3});
        manager.append(sprLoad);
        sprLoad.setPosition(getWidth() / 2 - 9, getHeight() / 2 + 40);
    }

    public void drawLogo(Graphics g) {
        g.setColor(Var.BLACK);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.drawImage(imgLogo, getWidth() / 2, 30, Graphics.HCENTER | Graphics.TOP);
    }

    public void drawLoading(Graphics g) {
        String[] strLoad = {"Loading   ", "Loading.  ", "Loading.. ", "Loading..."};
        for (int i = 0; i < 40; i++) {
            drawLogo(g);
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            sprLoad.nextFrame();
            manager.paint(g, 0, 0);
            flushGraphics();
            g.setColor(Var.WHITE);
            g.drawString(strLoad[i % 4], getWidth() / 2, getHeight() / 2 + 100, Graphics.HCENTER | Graphics.BASELINE);
            flushGraphics();
        }
    }

    public void start() {
        Thread t = new Thread(){
          public void run(){
              drawLoading(g);
          }
        };
        t.start();        
    }
}
